package cn.skyquant.quant4j.api.dto;

import java.io.Serializable;
import java.util.Objects;

public class ResultDTO<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    public final boolean ok;
    public final String message;
    public final T data;
    public static final ResultDTO success = ResultDTO.success();
    public static final ResultDTO error = ResultDTO.error("系统错误");
    public static final ResultDTO error1 = ResultDTO.error("参数错误");
    public static final ResultDTO error2 = ResultDTO.error("未找到数据");

    public static ResultDTO<Void> success(){
        return new ResultDTO(true,null,null);
    }

    public static ResultDTO<Void> error(){
        return new ResultDTO(false,null,null);
    }

    public static <T > ResultDTO<T> success(T data){
        return new ResultDTO(true,null,data);
    }

    public static <T> ResultDTO<T> error(String message){
        return new ResultDTO(false,message,null);
    }

    public static <T> ResultDTO<T> error(String message, T data){
        return new ResultDTO(false,message,data);
    }

    private ResultDTO(boolean ok, String message, T data) {
        this.ok = ok;
        this.message = message;
        this.data = data;
    }

    public T data(){
        return ok && data!=null?data:null;
    }

    public boolean okData(){
        return ok && data!=null;
    }

    @Override
    public String toString() {
        return "ResultDTO{" + ok +"," + message + ", data=" + data +"}";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof ResultDTO)) return false;
        ResultDTO<?> resultDTO = (ResultDTO<?>) o;
        return ok == resultDTO.ok &&
                Objects.equals(message, resultDTO.message) &&
                Objects.equals(data, resultDTO.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(ok, message, data);
    }
}
